﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;

namespace GeneralDashboard.Data.Core
{
    public abstract class ProtocolBase : INotifyCollectionChanged
    {
        protected readonly IDictionary<string, Type> _frameSequence;
        protected Dictionary<string, IDataUnit> _dataDict;

        public event NotifyCollectionChangedEventHandler CollectionChanged;

        public virtual void GetFrameData()
        {
            CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
        }
        public dynamic this[string index]
        {
            get
            {
                if (!_dataDict.ContainsKey(index))
                    return null;
                return _dataDict[index].GetValue();
            }
        }
        public ProtocolBase(IDictionary<string, Type> frameSequence)
        {
            _frameSequence = frameSequence;
            _dataDict = new Dictionary<string, IDataUnit>(_frameSequence.Count);
            foreach (var item in _frameSequence)
            {
                _dataDict.Add(item.Key, DataUnit.Create(item.Value));
            }
        }
    }
}
